package com.fesnakj.todoer.model;

import com.google.gwt.thirdparty.guava.common.base.Objects;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class TaskImpl implements HierarchicTask {

    private static Logger logger = Logger.getLogger(TaskImpl.class);

    private Integer id;
    private String name;
    private String description;
    private TaskState state = TaskState.NEW;
    private HierarchicTask parent;
    private List<HierarchicTask> subtasks = new ArrayList<HierarchicTask>();

    public TaskImpl(Integer id, String name) {
        this.id = id;
        this.name = name;
        logger.info("Created: " + this);
    }

    public TaskImpl(Integer id, String name, TaskState state) {
        this(id, name);
        this.state = state;
    }

    @Override
    public Integer getId() {
        return id;
    }

    @Override
    public String getTitle() {
        return name;
    }

    @Override
    public void setTitle(String name) {
        this.name = name;
    }

    @Override
    public String getDescription() {
        return description;
    }

    @Override
    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public TaskState getState() {
        return state;
    }

    @Override
    public void setState(TaskState state) {
        this.state = state;
    }

    @Override
    public HierarchicTask getParent() {
        return parent;
    }

    @Override
    public void setParent(HierarchicTask parent) {
        this.parent = parent;
        parent.getSubtasks.add(this);
    }

    @Override
    public List<HierarchicTask> getSubtasks() {
        return subtasks;
    }

    @Override
    public void addSubtask(HierarchicTask subtask) {
        if (subtask.equals(this)) throw new RuntimeException();
        subtask.setParent(this);
        logger.info("Added: " + subtask);
    }

    @Override
    public void addSubtasks(Collection<HierarchicTask> subtasks) {
        for (HierarchicTask hierarchicTask : subtasks) {
            addSubtask(hierarchicTask);
        }
    }

    @Override
    public void removeSubtask(HierarchicTask subtask) {
        this.subtasks.remove(subtask);
        logger.info("Removed: " + subtask);
    }

    @Override
    public void removeSubtasks(Collection<HierarchicTask> subtasks) {
        this.subtasks.removeAll(subtasks);
    }


    @Override
    public boolean equals(Object obj) {
        return ((BaseTask) obj).getId() == getId();
    }

    @Override
    public String toString() {
        return Objects.toStringHelper(this).add("id", getId()).add("title", getTitle()).add("state", getState()).toString();
    }

    public static void main(String[] args) {
        HierarchicTask task = new TaskImpl(1, "Title");
        System.out.println(task);
    }

}
